package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;

public class LipeiActivity extends BaseActivity {


    EditText contentTv, productNameTv;
    TextView companyTv;
    TextView right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipei);
        setActionBarTitle("理赔援助");
        right = findViewById(R.id.tv_bar_right);

        right.setText("一键理赔");
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(OneStepLiPeiActivity.class);
            }
        });


        companyTv = (TextView) findViewById(R.id.company);
        productNameTv = (EditText) findViewById(R.id.productName);
        contentTv = (EditText) findViewById(R.id.content);

        companyTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivityForResult(AllCompanyActivity.class, new String[]{"company_name"}, new String[]{""});
            }
        });


        findViewById(R.id.submit).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (loginStatus()) {


                            if (companyTv.getText().toString().trim().isEmpty()) {
                                showToast("公司名不能为空");
                                return;
                            }
                            if (productNameTv.getText().toString().trim().isEmpty()) {
                                showToast("产品名称不能为空");
                                return;
                            }
                            if (contentTv.getText().toString().trim().isEmpty()) {
                                showToast("描述内容不能为空");
                                return;
                            }
                            if (contentTv.getText().toString().trim().length() < 11) {
                                showToast("描述内容不能少于10个字");
                                return;
                            }


                            NetUtil net = new NetUtil(LipeiActivity.this, JsonApi.claim_help);
                            net.setParams("company", companyTv.getText().toString());
                            net.setParams("productName", productNameTv.getText().toString());
                            net.setParams("content", contentTv.getText().toString());

                            net.postRequest("", new NetUtil.RequestStringListener() {
                                @Override
                                public void onComplete(String result) {
                                    if (JSONUtil.isSuccess(result)) {
                                        showToast("提交成功");
                                        finish();
                                    }
                                }
                            });


                        }
                    }
                }
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            companyTv.setText(data.getStringExtra("company_name"));
        }
    }
}
